import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'modify_nickname_logic.dart';

class ModifyNickNamePage extends StatefulWidget {
  ModifyNickNamePage({Key? key}) : super(key: key);

  @override
  _ModifyNickNamePageState createState() => _ModifyNickNamePageState();
}

class _ModifyNickNamePageState extends State<ModifyNickNamePage> {
  final logic = Get.put(ModifyNickNameLogic());
  FocusNode focusNode = new FocusNode();
  @override
  void initState() {
    super.initState();

    ///添加获取焦点与失去焦点的兼听
    focusNode.addListener(() {
      ///当前兼听的 TextFeild 是否获取了输入焦点
      bool hasFocus = focusNode.hasFocus;

      ///当前 focusNode 是否添加了兼听
      bool hasListeners = focusNode.hasListeners;

      print("focusNode 兼听 hasFocus:$hasFocus  hasListeners:$hasListeners");
    });

    /// WidgetsBinding 它能监听到第一帧绘制完成，第一帧绘制完成标志着已经Build完成
    WidgetsBinding.instance.addPostFrameCallback((_) {
      ///获取输入框焦点
      FocusScope.of(context).requestFocus(focusNode);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('昵称'),
        actions: [
          TextButton(
            onPressed: () {
              // FocusScope.of(context).requestFocus(FocusNode());
              focusNode.unfocus();

              logic.modifyName(context);
            },
            child: Text('保存'),
          ),
        ],
      ),
      backgroundColor: Color(0xFFf4f6f7),
      body: Column(
        children: [
          SizedBox(height: 10.h),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w),
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            child: GetBuilder<ModifyNickNameLogic>(
              builder: (controller) => TextField(
                // controller: logic.controller,
                controller: TextEditingController.fromValue(
                  TextEditingValue(
                    text: controller.controller.text,
                    selection: TextSelection.fromPosition(
                      TextPosition(
                        offset: controller.controller.text.length,
                        affinity: TextAffinity.downstream,
                      ),
                    ),
                  ),
                ),

                focusNode: focusNode,
                keyboardType: TextInputType.name,
                style: TextStyle(color: Colors.black, fontSize: 14.sp),
                inputFormatters: [
                  LengthLimitingTextInputFormatter(10),
                  FilteringTextInputFormatter.singleLineFormatter,
                ],
                decoration: InputDecoration(
                  border: InputBorder.none,
                  suffixIcon: IconButton(
                    icon: Icon(Icons.cancel, color: Colors.grey),
                    onPressed: () {
                      controller.clearController();
                    },
                  ),
                ),

                onChanged: (value) {
                  controller.changeInPutText(value);
                },
                onSubmitted: (value) {
                  // FocusScope.of(context).requestFocus(FocusNode());
                  focusNode.unfocus();
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}
